package fr.ants.view.renderer.pixel;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import fr.ants.model.Nest;
import fr.ants.utils.MathUtils;

/**
 * Map of colors, randomly generated and associated to nests.
 *
 * @author Vianney DUPOY DE GUITARD.
 */
public class ColorMap {

	/** Static array of different colors. */
	private static final Color[] COLORS = new Color[] { new Color(196,39,4),new Color(0,34,255),
		new Color(255,187,0), new Color(4,196,196), new Color(0,87,12), new Color(216, 61, 255),
		new Color(255,0,0), new Color(4,39,196), new Color(0,255,17), new Color(145,4,196)};

	/** Map associating nests to colors. */
	private Map<Nest, Color> map = new HashMap<Nest, Color>();

	/** Current index in the static array of colors. */
	private int index = 0;

	/**
	 * Adds a nest to the mapping, and randomly chooses a color.
	 * @param n : the nest to add.
	 */
	public void addNest(Nest n) {
		map.put(n, randomColor());
	}

	/** @return the color associated to a nest. Generates one if needed. */
	public Color getColor(Nest n) {
		if (!map.containsKey(n)) {
			addNest(n);
		}
		return map.get(n);
	}

	/** @return a random color (not black nor white). */
	private Color randomColor() {
		Color color;
		if (index < COLORS.length) {
			color = COLORS[index];
			index++;
		} else {
			color = new Color(MathUtils.rnd(20, 220), MathUtils.rnd(20, 220), MathUtils.rnd(20, 220));
		}
		return color;
	}
}
